import com.alibaba.fastjson.JSONObject;
import com.xukehong.demo.util.encryption.MD5Utils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: xkh
 * @Date: 2019/11/25 11:17
 * @Version 1.0
 */
public class test1 {
    @Test
    public void test4() {
        String a = String.format("%s/%s", "http://qfcx247dt.hn-bkt.clouddn.com/", "74547745-9532-4b52-b701-fd8411ef56e4.txt");
        System.out.println(a);
    }

    public String a(){
       try {
           int i = 1/0;
           return "success";
       }catch (Exception e){
           return "exception";
       }
    }


    @Test
    public void test2() {
        String s = "(()())(())";
        String c = test3(s);
        System.out.println("获取的结果" + c);
    }

    private String test3(String s) {
        //记录左括号的数量
        int x = 0;
        //记录右括号的数量
        int y = 0;
        for (int i = 0; i < s.length(); i++) {


            if (x == 1) {
                s = s.substring(0, i - 1) + s.substring(i);
            }
            if (x == 0) {
                if (String.valueOf(s.charAt(i)).equals(String.valueOf(s.charAt(i + 1)))) {
                    if (String.valueOf(s.charAt(i)).equals("(")) {
                        x++;
                    }
                    if (String.valueOf(s.charAt(i)).equals(")")) {
                        y++;
                    }
                }
            } else {
                if (String.valueOf(s.charAt(i)).equals(String.valueOf(s.charAt(i + 1))) || String.valueOf(s.charAt(i - 1)).equals(String.valueOf(s.charAt(i)))) {
                    if (String.valueOf(s.charAt(i)).equals("(")) {
                        x++;
                    }
                    if (String.valueOf(s.charAt(i)).equals(")")) {
                        y++;
                    }
                }
            }


        }


//        int x = 0;
//        for (int i = 0; i < s.length(); i++) {
//            if(String.valueOf(s.charAt(i)).equals(")")|| String.valueOf(s.charAt(i)).equals("(")){
//                x++;
//            }else {
//                x = 0;
//            }
//           if(x == 2){
//               s = s.substring(0,i-1)+s.substring(i);
//           }
//        }
        return s;
    }


    @Test
    public void asa() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userFullName", "测试代理名字");
        jsonObject.put("superId", "8");
        jsonObject.put("number", "2");
        jsonObject.put("userPhone", "17785608004");
        jsonObject.put("userOutBalance", "2484.0");
        jsonObject.put("userPayBalance", "24844.0");
        jsonObject.put("userProBalance", "2000.0");

        JSONObject json1 = new JSONObject();
        json1.put("userFullName", "用户1");
        json1.put("superId", "8");
        json1.put("number", "2");
        json1.put("userPhone", "17785608004");
        json1.put("userOutBalance", "2484.0");
        json1.put("userPayBalance", "24844.0");
        json1.put("userProBalance", "2000.0");


        JSONObject json2 = new JSONObject();
        json2.put("userFullName", "用户1");
        json2.put("superId", "8");
        json2.put("number", "2");
        json2.put("userPhone", "17785608004");
        json2.put("userOutBalance", "2484.0");
        json2.put("userPayBalance", "24844.0");
        json2.put("userProBalance", "2000.0");

        List list = new ArrayList();
        list.add(json1);
        list.add(json2);
        jsonObject.put("list", list);
        System.out.println(jsonObject);
    }


    @Test
    public void jsoup() {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://www.qu.cn/man/yanshi/", String.class);
        Document document = Jsoup.parse(responseEntity.getBody());
        Elements elements = document.getElementsByClass("gl");
        System.out.println("结构一:=======================");
//        System.out.println(elements);
        for (Element link : elements) {
            Elements element = link.getAllElements();
            String img = StringUtils.substringBetween(element.toString(), "img src2=\"", "\"");
            String alt = StringUtils.substringBetween(element.toString(), "alt=\"", ">");
            String amount = StringUtils.substringBetween(element.toString(), "class=\"xj\">", "</b>");
            String title = StringUtils.substringBetween(element.toString(), "title=\"", "\"");
            System.out.println("标题:====================\n" + title);
            System.out.println("图片:====================" + img);
            System.out.println("alt:====================" + alt);
            System.out.println("金额:====================" + amount);
            JSONObject json = new JSONObject();
            json.put("goodsName", title);
            json.put("goodsImg", img);
            json.put("goodsDesc", alt);
            json.put("isSale", 1);
            json.put("goodsStatus", 1);
            json.put("goodsStock", 10);
            json.put("costPrice", amount);
            json.put("dataFlag", 1);
            json.put("platform", 7);
            json.put("createTime", new Date());
            json.put("commission", new BigDecimal(Math.random()).setScale(2, BigDecimal.ROUND_CEILING));
            request(json);

        }

    }

    public void request(JSONObject json) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.add("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ7XCJhZG1pbkxvZ0lwXCI6XCIxMjUuNzcuOTQuMTNcIixcImFkbWluTG9nTnVtXCI6Mjg5LFwiYWRtaW5Mb2dUaW1lXCI6MTU4NDYyNDY1MDAwMCxcImFkbWluTmFtZVwiOlwiYWRtaW5cIixcImFkbWluUGFzc1wiOlwiZmQyNjJmNmJmMjFiMDNhY2I3ODAyNDY3NzQ1MzllMmZcIixcImFkbWluU2VjcmV0XCI6XCI2UjVNUjdXRU1JU0I3Q1VWXCIsXCJhZG1pblN0YXRlXCI6MCxcImlkXCI6MX0iLCJpYXQiOjE1ODUyMDAyNDUsImV4cCI6MTU4NTMyMDI0NX0.0oR-kzvxEUkIz3yvb2Z1Nhs8ywCnKvN7PfaVRyM7qAAAeBHorhtScwVL4Wc1xN3RQQvvPBipK72s_zhHl5RULw");
        HttpEntity httpEntity = new HttpEntity<>(json.toString(), headers);
        ResponseEntity<String> response = restTemplate.exchange("http://192.168.0.216:8081/api/v2/goodsAdd", HttpMethod.POST, httpEntity, String.class);
        System.out.println(response.getBody());
    }

    @Test
    public void asd2() throws ParseException {
        String a = "15363\n" +
                "15376\n" +
                "15389\n" +
                "15454\n" +
                "15465\n" +
                "15469\n" +
                "15488\n" +
                "15490\n" +
                "15497\n" +
                "15512\n" +
                "15545\n" +
                "15548\n" +
                "15549\n" +
                "15559\n" +
                "15560\n" +
                "15561\n" +
                "15562\n" +
                "15572\n" +
                "15573\n" +
                "15586\n" +
                "15589\n" +
                "15605\n" +
                "15608\n" +
                "15612\n" +
                "15630\n" +
                "15640\n" +
                "15647\n" +
                "15648\n" +
                "15710\n" +
                "15741\n" +
                "15776\n" +
                "15797\n" +
                "15798\n" +
                "15800\n" +
                "15801\n" +
                "15803\n" +
                "15824\n" +
                "15828\n" +
                "15873\n" +
                "15881\n" +
                "15893\n" +
                "15894\n" +
                "15895\n" +
                "15896\n" +
                "15898\n" +
                "15899\n" +
                "15912\n" +
                "16024\n" +
                "16034\n" +
                "16087\n" +
                "16092\n" +
                "16127\n" +
                "16152\n" +
                "16279\n" +
                "16288";
        a = a.replace("\n", ",");
        System.out.println(a);

    }


    @Test
    public void pay() {
        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> maps = new HashMap<String, String>();
        maps.put("merchant_no", "20003"); //商户ID
        maps.put("pay_num", RandomStringUtils.randomNumeric(6));//上送订单号
        maps.put("total_fee", "10");//订单金额
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String data = simpleDateFormat.format(new Date());
        maps.put("today", data);//提交时间
        maps.put("pay_method", "101");//支付产品ID  微信：902 支付宝：904
        maps.put("merchant_userid", "20003");//支付产品ID  微信：902 支付宝：904
        maps.put("clientip", "127.0.0.1");//支付产品ID  微信：902 支付宝：904
        maps.put("return_url", "http://www.618mb.cn/success.html");//页面同步返回地址
        maps.put("notify_url", "http://www.618mb.cn/api/v0/thbNotify");//异步通知商户
        String toUrl = "" + maps.get("merchant_no") + maps.get("pay_num") + maps.get("total_fee") + maps.get("today") + "b94ce0103cef4fdd8ac294d43088d0da";
        String sign = MD5Utils.MD5Encode(toUrl, "UTF-8").toUpperCase();
        maps.put("sign", sign);
        maps.put("pay_method", "0");//支付产
        maps.put("return_type", "0");//异步通知商户
        maps.put("param", "");//异步通知商户
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        for (Map.Entry<String, String> entry : maps.entrySet()) {
            if (StringUtils.isNotBlank(entry.getValue())) {
                params.set(entry.getKey(), entry.getValue());
            }
        }
        System.out.println(params.toString());
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        ResponseEntity<String> results = restTemplate.exchange("http://www.bdfcoin.com/api/createPay", HttpMethod.POST, requestEntity, String.class);
        System.out.println("http://www.bdfcoin.com/" + results);

    }

    @Test
    public void pas() {
        String a = "1,2,3,4,5,6";
        List list = Arrays.asList(a.split(","));
        System.out.println(list.get(list.size() - 6));
    }
}
